package com.dykj.fireFighting.web.mq;

import com.alibaba.fastjson.JSON;
import com.dykj.fireFighting.api.elasticsearch.devHis.entity.EsDevHis;
import com.dykj.fireFighting.api.elasticsearch.devHis.service.EsDevHisService;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.entity.DevHis;
import com.dykj.fireFighting.web.message.InsertDevHisMessage;
import com.dykj.fireFighting.web.service.DevHisService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhengbo
 */
@Service
@RocketMQMessageListener(
        topic = InsertDevHisMessage.TOPIC,
        consumerGroup = "dy-consumer-group-" + InsertDevHisMessage.TOPIC
)
public class InsertDevHisConsumer implements RocketMQListener<InsertDevHisMessage> {
   public static final Logger LOGGER = LoggerFactory.getLogger(InsertDevHisConsumer.class);
   @Autowired
   private RedisService redisService;
   @Autowired
   private DevHisService devHisService;
   @DubboReference
   private EsDevHisService esDevHisService;
    @Override
    public void onMessage(InsertDevHisMessage insertDevHisMessage) {
        DevHis devHis = insertDevHisMessage.getDevHis();
        LOGGER.info("===========插入devHis数据到mysql==============");
        devHisService.insertHis(devHis);
        LOGGER.info("===========插入EsDevHis数据到ES==============");
        EsDevHis esDevHis = new EsDevHis();
        BeanUtils.copyProperties(devHis, esDevHis);
        esDevHis.setUnitId(devHis.getUnitsCode());
        esDevHis.setDeviceName(devHis.getDevName());
        esDevHis = esDevHisService.importAll(esDevHis);
        LOGGER.info("===========插入EsDevHis数据到ES:=============" + JSON.toJSONString(esDevHis));

        redisService.set(devHis.getId(), JSON.toJSONString(devHis), 3600L);
    }
}
